<%@ include file="/WEB-INF/jsp/includes/include.jsp" %>
<%@ include file="/WEB-INF/jsp/includes/header.jsp" %>
<h2>Open Auctions:</h2>

<c:url var="actionUrl" value="/postings/search.html"  />
<form:form action="${actionUrl}" modelAttribute="posting">
	<fieldset class="row1">
		<div class="form-pair">	
			<div class="form-item">
				<label for="searchbytitle">Search by posting's title:</label>
			</div>
			<div class="form-value">
				<form:input path="title" size="30" maxlength="30"/> 
 			</div>
		</div>
	</fieldset>
	<fieldset class="form-submit">
		<div class="form-submit-buttons">
			<input type="submit" class="input-submit" name="search" value="search" />
		</div>
	</fieldset>
</form:form>

<table cellspacing="0" cellpadding="5" border="1">
	<thead>
		<tr>
			<th>NAME</th>
			<th>DESCRIPTION</th>
			<th>RESERVE PRICE</th>
			<th>CURRENT BID</th>
			<th>SELLER INFO</th>
			<th>START DATE</th>
			<th>END DATE</th>
		</tr>
	</thead>
	<tbody>
	<c:forEach var="posting" items="${postingList}" varStatus="rowCounter">
	        <c:choose>
	          <c:when test="${rowCounter.count % 2 == 0}">
	            <c:set var="rowStyle" scope="page" value="row1"/>
	          </c:when>
	          <c:otherwise>
	            <c:set var="rowStyle" scope="page" value="row2"/>
	          </c:otherwise>
	        </c:choose>
			<tr class="${rowStyle}">
				<td><a href="<c:url value="/postings/show/${posting.id}/"/>"><c:out value="${posting.title}" escapeXml="true" /></a></td>
				<c:if test="${fn:length(posting.description) > 30}">
				<td><c:out value="${fn:substring(posting.description,0,30)}..." escapeXml="true" /></td>
				</c:if>
				<c:if test="${fn:length(posting.description) <= 30}">
				<td><c:out value="${posting.description}" escapeXml="true" /></td>
				</c:if>
				<td>$${posting.price}</td>
				<td>
					<c:choose>
			          <c:when test="${fn:length(posting.bids) > 0}">
			            <c:forEach var="bid" items="${posting.bids}" varStatus="bidCounter">
							<c:if test="${bidCounter.count == 1}">
								<c:out value="$${bid.price}" escapeXml="true" />
							</c:if>
						</c:forEach>
			          </c:when>
			          <c:otherwise>
			            not placed yet
			          </c:otherwise>
			        </c:choose>
				</td>
				<td>${posting.user.firstName} ${fn:substring(posting.user.lastName, -fn:length(posting.user.lastName)-1, 1)}.</td>
				<td><fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${posting.startDate}" /></td>
				<td><fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${posting.endDate}" /></td>
			</tr>
	</c:forEach>
	<c:if test="${postingList == null}">
		<td colspan="7" align="center" class="error">No postings found.</td>
	</c:if>
	</tbody>
</table>
<%@ include file="/WEB-INF/jsp/includes/footer.jsp" %>